package com.mrper.ext.dialog;

import android.app.Activity;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;

import com.miguo.framework.view.LoadingDialog;
import com.miguo.ui.R;
import com.miguo.ui.databinding.DialogShareLinkActionsheetBinding;
import com.miguo.ui.databinding.GriditemShareActionsheetBinding;
import com.mrper.ext.adapter.TempleteAdapter;
import com.mrper.ext.annotation.ContentView;
import com.mrper.ext.uibase.BasicDialogFragment;
import com.mrper.model.share.ShareLinkParamsInfoEntry;
import com.mrper.util.DialogUtil;
import com.mrper.util.share.ShareCallback;
import com.mrper.util.share.ShareLinkUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2016/8/26.
 * 分享链接的ActionSheet对话框
 */
@ContentView(R.layout.dialog_share_link_actionsheet)
public class ShareLinkActionSheetDialog extends BasicDialogFragment<DialogShareLinkActionsheetBinding> implements AdapterView.OnItemClickListener {

    public static final String TAG = ShareLinkActionSheetDialog.class.getSimpleName() + System.currentTimeMillis();

    public static final int[] SHARE_TYPE_ICON = {
            R.drawable.ic_share2wechatfriend,
            R.drawable.ic_share2wechat,
            R.drawable.ic_share2qzone,
//            R.drawable.ic_share2sinaweibo,
            R.drawable.ic_share2qqfriend
    };

    public static final int[] SHARE_TYPE_TEXT = {
            R.string.dialog_shareActionSheet_share2Wechat,
            R.string.dialog_shareActionSheet_share2WechatFriends,
            R.string.dialog_shareActionSheet_share2QZone,
//            R.string.dialog_shareActionSheet_share2SinaWeibo,
            R.string.dialog_shareActionSheet_share2QQ
    };

    private Activity shareActivity;
    private ShareCallback shareCallback;
    private ShareLinkParamsInfoEntry shareParams;
    private LoadingDialog loadingDialog;

    /** 设置要分享的参数对象  **/
    public void setShareParams(ShareLinkParamsInfoEntry params){
        this.shareParams = params;
    }

    public void setShareActivity(Activity activity){
        this.shareActivity = activity;
        this.loadingDialog = new LoadingDialog(shareActivity);
    }

    @Override
    protected void initViews() {
        super.initViews();
        dataBinder.setOnClickEvent(this);
        List<HashMap<String,Object>> shareTypeMap = new ArrayList<>();
        //添加分享项
        for(int i=0;i<SHARE_TYPE_ICON.length;i++){
            HashMap<String,Object> item = new HashMap<>();
            item.put("icon",SHARE_TYPE_ICON[i]);
            item.put("text",SHARE_TYPE_TEXT[i]);
            shareTypeMap.add(item);
        }
        dataBinder.gvShare.setAdapter(new TempleteAdapter<>(activity, R.layout.griditem_share_actionsheet, shareTypeMap,
                new TempleteAdapter.OnInitAdapterListener<GriditemShareActionsheetBinding, HashMap<String, Object>>() {
                    @Override
                    public void initSetValue(GriditemShareActionsheetBinding binder, HashMap<String, Object> item, int position) {
                        binder.txtShareTypeText.setText(getString(Integer.valueOf(item.get("text").toString())));
                        binder.imgShareTypeIcon.setImageResource(Integer.valueOf(item.get("icon").toString()));
                    }
                }));
        dataBinder.gvShare.setOnItemClickListener(this);
    }

    @Override
    protected int getDialogAnimationId() {
        return R.style.ActionSheetAnimationStyle;
    }

    @Override
    protected int getDialogGravity() {
        return Gravity.BOTTOM;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(shareActivity == null){
            Log.e(ShareLinkActionSheetDialog.class.getSimpleName(), "请设置shareActivity的实例");
            return;
        }
        if(shareCallback == null){
            shareCallback = new ShareCallback(shareActivity);
            shareCallback.setLoadingDialog(loadingDialog);
        }
        DialogUtil.showDialog(loadingDialog); //显示加载对话框
        if(shareParams != null) {
            int targetType = ShareLinkUtil.TYPE_WX;
            switch (position) {
                case 0://分享到微信好友
                    shareParams.scene = 0;
                    break;
                case 1://分享到微信
                    shareParams.scene = 1;
                    break;
                case 2://分享到QQ空间
                    targetType = ShareLinkUtil.TYPE_QQ;
                    shareParams.scene = 0;
                    break;
                case 3://分享到QQ好友
                    targetType = ShareLinkUtil.TYPE_QQ;
                    shareParams.scene = 1;
                    break;
            }
            ShareLinkUtil.shareTo(shareActivity, targetType, ShareLinkParamsInfoEntry.toJSON(shareParams), shareCallback);
        }
        this.dismiss();
    }

    public void onClick(View v) {
        this.dismiss();
    }

}
